package com.xzsoft.gdwz.schedual;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.xzsoft.gdwz.model.OrderHeader;
import com.xzsoft.gdwz.services.ConfirmOrCancleOrder;
import com.xzsoft.gdwz.services.JdOrderService;
import com.xzsoft.gdwz.services.OrderReturnServices;

/**
 * 
 * <p>Title: </p>
 * <p>Description:订单的确认和取消(定时任务)</p>
 * <p>Company:兴竹信息 </p>
 * @author:杨   光
 * @date:2016-8-24 下午2:29:44
 */
@Component
public class OrderReturnServicesImpl implements OrderReturnServices {

	Logger logger = Logger.getLogger(OrderReturnServicesImpl.class);

	@Autowired
	private JdOrderService jdOrderService;
	
	@Autowired
	private ConfirmOrCancleOrder confirmOrCancleOrder;
	
	@Scheduled(cron="0 5/10 * * * ?")
	@Override
	public void RefrshOrderOnTime(){
		System.out.println("执行确认和取消订单定时任务！");
		List<OrderHeader> orderHeaderList = new ArrayList<OrderHeader>();
		orderHeaderList = jdOrderService.getOrder();
		System.out.println(orderHeaderList.toString());
		for (int i = 0; i < orderHeaderList.size(); i++) {
			String inqstatus = orderHeaderList.get(i).getInqstatus();
			String ecommerceOrderId = orderHeaderList.get(i).getEcommerceOrderId();
			String ecommercename = orderHeaderList.get(i).getEcommerceName();
			String inqid = orderHeaderList.get(i).getInqHid();
			String ecommerceUserName = orderHeaderList.get(i).getEcommerceUserName();
			boolean result = false;
			if (inqstatus.equals("G11")) {
				if(ecommercename.equals("JD")){
					logger.info("调用京东确认订单！");
					result = confirmOrCancleOrder.JDConfirmOrder(ecommerceOrderId);
				}else if(ecommercename.equals("SN")){
					logger.info("调用苏宁确认订单！");
					result = confirmOrCancleOrder.SNConfirmOrder(ecommerceOrderId); 
				}else if(ecommercename.equals("DL")){
					logger.info("调用得力确认订单！");
					result = confirmOrCancleOrder.DLConfirmOrder(ecommerceOrderId);
				}else if(ecommercename.equals("XY")){
					logger.info("调用西域确认订单！");
					result = confirmOrCancleOrder.
							XYConfirmOrConcleOrder(ecommerceOrderId, "1");
				}else{
					result = false;
				}
				
				if (result) {
					jdOrderService.UpdateOrderStatus(inqid);
				} else {
					logger.info("订单确认失败!");
				}
				
			} else if (inqstatus.equals("G12")||inqstatus.equals("D1")||inqstatus.equals("D2")) {
				logger.info("信息查询成功");
				if(ecommercename.equals("JD")){
					logger.info("调用京东取消订单！");
					result = confirmOrCancleOrder.JDCancleOrder(ecommerceOrderId);
				}else if(ecommercename.equals("SN")){
					logger.info("调用苏宁取消订单！");
					result = confirmOrCancleOrder.SNCancleOrder(ecommerceOrderId);
				}else if(ecommercename.equals("DL")){
					logger.info("调用得力取消订单！");
					result = confirmOrCancleOrder.DLCancleOrder(ecommerceOrderId);
				}else if(ecommercename.equals("XY")){
					logger.info("调用西域取消订单！"+ecommerceUserName);
					result = confirmOrCancleOrder.
							XYConfirmOrConcleOrder(ecommerceOrderId, "0");
				}else{
					result = false;
				}
				
				if (result) {
					jdOrderService.UpdateOrderStatus(inqid);
				} else {
					logger.info("订单取消失败!");
				}
			} else {
				    logger.info("订单状态不对!");
				    continue;
			}
		}
	}

}
